from pymysql import Connection

# 获取mysql的连接对象
conn = Connection(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='toor',
    database='111',
    charset='utf8',
    autocommit=True  # 设置自动提交事务
)
# 打印mysql数据库的软件信息
print(conn.get_server_info())

# 执行非查询性质的SQL
# 1、获取游标对象
cursor = conn.cursor()
# 2、创建表
cursor.execute('CREATE TABLE IF NOT EXISTS user(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20))')
# 3、插入数据
cursor.execute("INSERT INTO `user`(`name`) VALUE('aadsxx')")
# conn.commit() # 确认更改，提交事务，或者设置为自动提交

# 执行查询性SQL
cursor.execute('SELECT * FROM user')
# 获取查询结果
result = cursor.fetchall()  # 对象为元组
# 循环打印
for row in result:
    print(row)

# 关闭连接数据库
conn.close()
